package com.wn.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class TokenUtils {

    public static Long getTokenUserId() {

        /**
         *   gateway 中 把 token 进行校验、后续的操作中不需要 解析token 直接头重写中获取 userId
         *
         *   RequestContextHolder  全局封装 request 的类  这里可以直接获取 HttpServletRequest
         */
        HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 获取 头重写的 userId
        String userId = request.getHeader("userId");
        return Long.parseLong(userId);
    }
}